// 测试环境
// var base_url = 'https://qdzwapp.qingdao.gov.cn:7118';
// var base_url = '/dev-fund-api';
// 生产环境
// var base_url = 'https://xianmaopms.com';

var noLoading_url = [
	'/auth/app/app/getPicBase64',
	'/app/app/getPersonStream',
	'/hz-admin/dg/service650', //取叫号
	'/app/basicinfo/yesterday',
	'/app/basicinfo/overTim',
	'/app/evaluate/getEvaluateStatistics',
	'/app/app/monitorItem',
	'/app/app/getEventNum',
	'/app/app/specialPopulationStatistics',
	'/app/data/inner-warning',
	'/app/app/getZoneNumber',
	'/app/data/getPersonSteamByAreaDict',
	'/app/data/getWarningNumByAreaDict'
]

export default {
	config: {
		// #ifdef APP-PLUS || MP
		baseUrl: 'http://124.70.89.189:3306',
		// #endif
		// #ifdef H5
		baseUrl: '',
		// #endif
		// baseUrl: process.env.NODE_ENV === 'development' ? '' : 'http://124.70.89.189:3306',
		headers: {
			"content-type": "application/json",
			"Authorization": "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
			'Tenant-Id': '000000',
			"platform": "app",
			//"content-type": "multipart/form-data",
		},
		dataType: "json",
		responseType: "text"
	},
	interceptor: {
		request: function (_config) { },
		response: null
	},
	request(options) {
		if (noLoading_url.indexOf(options.url) == -1) {
			console.log('re3', options.url);
			uni.showLoading({
				title: "加载中",
				mask: true,
			});
		}

		return new Promise((resolve, reject) => {
			let _config = null
			var baseUrl = this.config.baseUrl
			options.url = baseUrl + options.url
			options.header = Object.assign(this.config.headers, options.header || {})
			// options.headers['Authorization'] = `Basic c2FiZXI6c2FiZXJfc2VjcmV0`;
			// options.headers['Tenant-Id'] = '000000'
		
			if (uni.getStorageSync('token')) {
				options.header['Blade-Auth'] = 'bearer ' + uni.getStorageSync('token') 
			} else {
				if (!getCurrentPages().route == "pages/login/login") {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			}
			options.header = Object.assign(this.config.headers, options.header)
			options.complete = (response) => {
				
				setTimeout(() => {
					uni.hideLoading()
				}, 500);
				uni.stopPullDownRefresh();
				response.config = _config
				if (response.statusCode == 200) { //成功
					resolve(response.data);
				} else {
					if (options.showtips == "show") {
						uni.showToast({
							icon: "none",
							title: response.data.msg
						})
					}
					reject(response)
				}
			}
			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()
			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}

			uni.request(_config);
		});
	},
	get(url, data, showtips, options) {
		data = data ? data : {};
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.header = {...this.config.headers, ...options}
		if (uni.getStorageSync('token')) {
			options.header['Blade-Auth'] = 'bearer ' + uni.getStorageSync('token') 
		} else {
			if (!getCurrentPages().route == "pages/login/login") {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
		options.method = 'GET'
		options.showtips = showtips
		return this.request(options)
	},
	post(url, data, showtips, options) {
		data = data ? data : {};
		if (!options) {
			options = {}
		}
		options.he
		options.url = url
		options.data = data
		options.header = {...this.config.headers, ...(options || {})}
		if (uni.getStorageSync('token')) {
			options.header['Blade-Auth'] = 'bearer ' + uni.getStorageSync('token') 
		} else {
			if (!getCurrentPages().route == "pages/login/login") {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
		options.method = 'POST'
		options.showtips = showtips
		return this.request(options)
	},
	put(url, data, showtips, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.header = {...this.config.headers, ...options}
		if (uni.getStorageSync('token')) {
			options.header['Blade-Auth'] = 'bearer ' + uni.getStorageSync('token') 
		} else {
			if (!getCurrentPages().route == "pages/login/login") {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
		options.method = 'PUT'
		options.showtips = showtips
		return this.request(options)
	},
	del(url, data, showtips, options) {
		data = data ? data : {};
		if (!options) {
			options = {}
		}
		options.he
		options.url = url
		options.data = data
		options.header = {...this.config.headers, ...options}
		if (uni.getStorageSync('token')) {
			options.header["Authorization"] = 'bearer ' + uni.getStorageSync('token') 
		} else {
			if (!getCurrentPages().route == "pages/login/login") {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
		options.method = 'DELETE'
		options.showtips = showtips
		return this.request(options)
	},
	uploadFile(path) {
		return new Promise(function (resolve, reject) {
			uni.showLoading({
				title: "文件上传中",
				mask: true,
			});
			uni.uploadFile({
				url: 'http://120.27.27.46:22001/dev-fund-api/system/file/imgUpload',
				filePath: path,
				header: {
					Authorization: uni.getStorageSync('token'),
					...this.config.headers
				},
				formData: {},
				complete: (uploadFileRes) => {
					console.log('promise', uploadFileRes);
					uni.hideLoading()
					if (uploadFileRes.statusCode == 200) {
						resolve(JSON.parse(uploadFileRes.data))
					}

				}
			})
		})
	}
}
